#PHP Note ##变量
- 区分大小写
- 没有声明变量
- 在函数外定义的变量为全局,函数内定义的变量为局部 全局变量保存在 ¥GLOBALS[index] 里,函数内调用全局变量需要使用 globals 关键字, 如¥GLOBALS[‘test’] 调用全局变量¥test
- 在函数内使用 static 关键字声明变量,该变量不会被删除,而在下一次被调用的时候保存上一次被调用时的值
<?php
$test = 1;
function myTest(){
static $x = 0; //下一次调用的时候保存上一次调用时候的值
echo $x;
$x = $x + $GLOBALS["test"]; //函数内调用全局变量
}
myTest(); myTest(); myTest()
?>
##输出 ###echo 输出字符串,可以包含HTML标签,用逗号连接不同的内容 echo $txt1; echo "
"; echo "Study PHP at $txt2"; echo "My car is a {$cars[0]}"; ###print 有一个特殊的返回值“1”,比echo要慢 print $txt1; print "
"; print "Study PHP at $txt2"; print "My car is a {$cars[0]}"; **printf** :printf(format,arg1,arg2,arg++) ```php <?php $number = 9; $str = "Beijing"; printf("There are %u million bicycles in %s.",$number,$str); ?> ```
var_dump()
可以显示变量的类型
##数据类型 ###常量 Constant 全局固定标识符,不能被改变,不需要¥符号 define(“GREETING”, “欢迎访问 Runoob.com”, true); echo greeting; ###字符串 String 并置运算符**(.)来拼接字符串 如 ¥string1.“ ”.¥string2 函数strlen(string)** 用来计算字符串的长度 函数**strpos(string, keyword)**用来在一段字符串中查找keyword,将返回第一个匹配的字符位置,或者false
HereDoc: 通过开始标示符和结束标识符来写一段字符串,不用考虑拼接和转译,标识符可以任取,确保不在字符串中出现即可,如下:
<?php
$user="whoami";
$form_str = <<<HAHA
<form>
<input type="text" placeholder="account" value="$user">
<input type="password" placeholder="password">
<input type="submit" value="Login">
</form>
HAHA;
echo $form_str;
?>
###数组 Array
<?php
$cars=array("Volvo","BMW","Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
函数**count(Array)**用来返回array的长度 关联数组:分配给数组指定的键的数组
<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
函数**sort(Array)用来升序排列 函数rsort(Array)用来降序排列 函数asort(Array)根据数组的值升序排列 函数ksort(Array)**根据数组的键升序排列
Function
代码重用
代码重用是指将一个文件引入当前PHP脚本中,由require 和include 两种方法组成
- require():失败后会给出一个fatal error
- include():失败后会给出一个warning,不影响程序
函数原型
resource fopen (string filename, string mode
[, bool use_include_path [, resource context)
resource 是函数返回类型; 方括号说明该参数可选 函数名不区分大小写
参数传递
- 值传递:
function test($a){...}仅传递值,不修改值 - 引用传递:
function test(&$a){...}传递引用,会修改值
面向对象
class 类名{
public $attribute; //定义类的属性
public function fun(){ //定义类的方法
//定义该方法所要实现的功能
}
}
构造函数
当创建一个对象时,将调用其构造函数,执行一些有用的初始化任务,比如初始值或者创建该对象需要的其他对象。
<?php
class car{
public $color;
public $size;
public function __construct($x,$y){ //通过构造函数对类体中的属性进行初始化
$this->color=$x;
$this->size=$y;
}
public function printAttribute(){
echo "颜色:".$this->color;
echo " 大小:".$this->size."米";
}
}
$bus=new car("blue","20"); //在实例化对象时为类体中属性指定初始值
$bus->printAttribute();
?>
属性封装
不建议从类的外部直接访问类的属性。可以通过使用__get() 和 __set() 的方法实现对属性的访问,例如
class test
{
public $attributel;
function __get($name)
{
return $this->$name;
}
function __set($name, $value)
{
$this->$name = $value;
}
}
$a = new test();
$a->$attribute = 5; // 自动调用set 函数
echo $a->$attribute; // 自动调用get 函数
这里的get 和set 不会直接被调用,因为有__ 下划线在,所以是内置自动执行的函数。
有意义的例子,用于检查属性和值的范围:
function __set($name, $value) {
if(($name=="attribute") && ($value>0) && ($value<=100))
{
$this->$attribute = $value;
}
}
域运算符(::)
在没有声明任何实例的情况下访问类中的函数或者基类中的函数和变量很有用处。而*:😗 运算符即用于此情况。
<?php
class A {
function example() {
echo "I am the original function A::example().<br />\n";
}
}
class B extends A {
function example() {
echo "I am the redefined function B::example().<br />\n";
A::example();
}
}
// A 类没有对象,这将输出
// I am the original function A::example().<br />
A::example();
// 建立一个 B 类的对象
$b = new B;
// 这将输出
// I am the redefined function B::example().<br />
// I am the original function A::example().<br />
$b->example();
?>
继承
php 通过关键字 extends 来指明继承关系
class A
{
public $attr1;
function operation1(){};
}
class B extends A
{
public $attr2;
function operation2(){};
}
$b = new B();
$b->$attr1, ->$attr2, ->$operation1(), ->$operation2() 都是有效的
- private 属性和方法在外部不可见,也不可以继承
- protected 属性和方法在外部不可见,但可以继承
- PHP 不支持多重继承
重载
子类重新定义父类的属性和方法,叫做重载。重载不影响父类
final 关键字
在一个函数声明前使用final 关键字,这个函数将不能被任何子类所重载
接口
接口指定一个实现了该接口的类必须实现的一系列函数。这样变通的让不能多重继承的类,在继承一个父类基础上,实现多个接口。
class Displayable()
{
function display();
}
class WebPage implements Displayable
{
function display()
{
//...
}
}
Cookie & Session
Cookie
保存在客户端,用来保存不重要的信息。
<?php
$exam="PHP Cookie测试!";
setcookie("test",$exam);
setcookie("test",$exam,time()+1800); //0.5小时失效
echo $_COOKIE['test'];
?>
Session
保存在服务器,占服务器资源,保存重要信息
<?php
$_SESSION["session_name"]="session_value"; //新建session
if(!empty($_SESSION['session_name'])) //调用session
$myvalue=$_SESSION['session_name'];
unset($_SESSION['session_name']); //删除session
?>
##MySQL